import Cookies from "js-cookie"
import Vue from "vue"
const app = {
    state: {
        sidebar: {
            opened: false
        },
        screenHide: false,
        visitedViews: [],
        themeName: "black"
    },
    mutations: {
        FULLSCREENHIDE: (state, status) => {
            console.log(status)
            state.screenHide = status
        },
        TOGGLE_SIDEBAR: (state, status) => {
            if (state.sidebar.opened) {
                Cookies.set("sidebarStatus", 1)
            } else {
                Cookies.set("sidebarStatus", 0)
            }
            if (status !== undefined) {
                state.sidebar.opened = status
            } else {
                state.sidebar.opened = !state.sidebar.opened
            }
        },
        ADD_VISITED_VIEWS: (state, view) => {
            if (state.visitedViews.some(v => v.path === view.path)) return
            state.visitedViews.push({
                name: view.name,
                path: view.path
            })
        },
        DEL_VISITED_VIEWS: (state, view) => {
            let index
            for (const [i, v] of state.visitedViews.entries()) {
                if (v.path === view.path) {
                    index = i
                    break
                }
            }
            state.visitedViews.splice(index, 1)
        },
        CHANGE_THEMENAME: (state, themeName) => { // 改变主题名字
            Vue.set(state, "themeName", themeName)
        },
        SETGTIME: (state, gTime) => {
            if (gTime.length !== 0) {
                // 状态为string 时间
                Vue.set(state, "gTime", gTime)
                Vue.localStorage.set("gTime", JSON.stringify(gTime))
            } else {
                // 状态为string 时间
                Vue.set(state, "gTime", [{
                        0: "",
                        1: ""
                    }])
                    // 存stirng时间
                Vue.localStorage.set("gTime", JSON.stringify(gTime))
            }
        },
        SETSCREENOBJ: (state, screenObj) => {
            Vue.set(state, "screenObj", screenObj)
        }
    },
    actions: {
        FullScreenHide({
            commit
        }, bool) {
            commit("FULLSCREENHIDE", bool)
        },
        ToggleSideBar({
            commit
        }, bool) {
            commit("TOGGLE_SIDEBAR", bool)
        },
        addVisitedViews({
            commit
        }, view) {
            commit("ADD_VISITED_VIEWS", view)
        },
        delVisitedViews({
            commit,
            state
        }, view) {
            return new Promise((resolve) => {
                commit("DEL_VISITED_VIEWS", view)
                resolve([...state.visitedViews])
            })
        },
        changeThemeName({
            commit,
            state
        }, themeName) {
            commit("CHANGE_THEMENAME", themeName)
        },
        // 设置全局时间
        setGTime({
            commit,
            state
        }, gTime) {
            commit("SETGTIME", gTime)
        },
        // 设置selTime ,提供面包屑导航
        setScreenObj({
            commit,
            state
        }, screenObj) {
            commit("SETSCREENOBJ", screenObj)
        }
    }
}

export default app